/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include "gtest/gtest.h"
#include "mockcpp/mockcpp.hpp"
#include "LivemountServiceJob.h"

using namespace Module;

static AppProtect::LivemountJob LivemountJobSetup()
{
    AppProtect::LivemountJob livemountJob;
    livemountJob.requestId = "reqId123";
    livemountJob.jobId = "jId123";
 
    std::string path = "/MetaFS/cache";
    HostAddress hostadd;
    hostadd.ip="10.28.12.144";
    StorageRepository cacheRepo;
    cacheRepo.repositoryType = RepositoryDataType::CACHE_REPOSITORY;
    cacheRepo.path.push_back(path);
    cacheRepo.remotePath = "/MetaFS/cache";
    cacheRepo.remoteHost.push_back(hostadd);
    cacheRepo.auth.authkey = "admin";
    cacheRepo.auth.authPwd = "Admin@123";
    cacheRepo.endpoint.ip = "10.28.12.144";
    cacheRepo.endpoint.port = 8088;
 
    StorageRepository dataRepo;
    dataRepo.repositoryType = RepositoryDataType::DATA_REPOSITORY;
    dataRepo.remotePath = "/BackupFS";
    dataRepo.path.push_back(dataRepo.remotePath);
    dataRepo.remoteHost.push_back(hostadd);
    dataRepo.auth.authkey = "admin";
    dataRepo.auth.authPwd = "Admin@123";
    dataRepo.endpoint.ip = "10.28.12.144";
    dataRepo.endpoint.port = 8088;
 
    StorageRepository metaRepo;
    metaRepo.repositoryType = RepositoryDataType::META_REPOSITORY;
    path = "/MetaFS/meta";
    metaRepo.path.push_back(path);
    metaRepo.remotePath = "/MetaFS/meta";
    metaRepo.remoteHost.push_back(hostadd);
    metaRepo.auth.authkey = "admin";
    metaRepo.auth.authPwd = "Admin@123";
    metaRepo.endpoint.ip = "10.28.12.144";
    metaRepo.endpoint.port = 8088;
 
    livemountJob.copy.repositories.push_back(cacheRepo);
    livemountJob.copy.repositories.push_back(dataRepo);
    livemountJob.copy.repositories.push_back(metaRepo);
 
    livemountJob.extendInfo = "{\n\t\"dstPath\" : \"/opt\",\n\t\"fileSystemShareInfo\" : \n\t[\n\t\t{\n\t\t\t\"accessPermission\" : 1,\n\t\t\t\"advanceParams\" : \n\t\t\t{\n\t\t\t\t\"clientName\" : \"*\",\n\t\t\t\t\"clientType\" : 0,\n\t\t\t\t\"portSecure\" : 1,\n\t\t\t\t\"rootSquash\" : 1,\n\t\t\t\t\"squash\" : 1\n\t\t\t},\n\t\t\t\"fileSystemName\" : \"fileset_mount_202207251733\",\n\t\t\t\"type\" : 1\n\t\t}\n\t],\n\t\"performance\" : {}\n}\n";
    return livemountJob;
}

static AppProtect::SubJob GetSubJob()
{
    AppProtect::SubJob subJob;
    subJob.jobId = "jobId123";
    subJob.subJobId = "subJob123";
    subJob.jobName = "subJob";
    subJob.jobPriority = 1;
    subJob.ignoreFailed = true;
    subJob.execNodeId = "abcde";
    subJob.jobInfo = "{\"ControlFile\":\"/cache/livemount-job/scan/ctrl\","
                    "\"SubTaskType\":1,"
                    "\"Ext\":\"abc\"}";
    return subJob;
}

class LivemountServiceJobTest : public testing::Test {
public:
    void SetUp() {
        m_livemountServiceJob = make_unique<LivemountServiceJob>();
    };
    void TearDown() {
        GlobalMockObject::verify(); // 校验mock规范并清除mock规范
    };

    void SetJobInfo() {
        AppProtect::LivemountJob livemountJob = LivemountJobSetup();
        auto jobInfoLivemount = std::make_shared<JobCommonInfo>(std::make_shared<LivemountJob>(livemountJob));
        m_livemountServiceJob->SetJobInfo(jobInfoLivemount);
        AppProtect::SubJob subJob = GetSubJob();
        m_livemountServiceJob->SetSubJob(std::make_shared<SubJob>(subJob));
        m_livemountServiceJob->SetParentJobId("parent_0");
    }
private:
    std::unique_ptr<LivemountServiceJob> m_livemountServiceJob;
};

/*
 * 用例名称: GetJobInfoBody
 * 前置条件：
 * check点：获取任务信息
 */
TEST_F(LivemountServiceJobTest, GetJobInfoBody)
{
    EXPECT_EQ(m_livemountServiceJob->GetJobInfoBody(), false);
    SetJobInfo();
    EXPECT_EQ(m_livemountServiceJob->GetJobInfoBody(), true);
}

/*
 * 用例名称: CheckBackupJobType
 * 前置条件：
 * check点：检查任务类型成功
 */
TEST_F(LivemountServiceJobTest, CheckBackupJobType)
{
    EXPECT_EQ(m_livemountServiceJob->CheckBackupJobType(), SUCCESS);
}

/*
 * 用例名称: PrerequisiteJob
 * 前置条件：
 * check点：前置任务成功
 */
TEST_F(LivemountServiceJobTest, PrerequisiteJob)
{
    EXPECT_EQ(m_livemountServiceJob->PrerequisiteJob(), SUCCESS);
}

/*
 * 用例名称: GenerateSubJob
 * 前置条件：
 * check点：生成子任务
 */
TEST_F(LivemountServiceJobTest, GenerateSubJob)
{
    EXPECT_EQ(m_livemountServiceJob->GenerateSubJob(), FAILED);
    SetJobInfo();
    EXPECT_EQ(m_livemountServiceJob->GenerateSubJob(), SUCCESS);
}

/*
 * 用例名称: ExecuteSubJob
 * 前置条件：
 * check点：执行子任务
 */
// TEST_F(LivemountServiceJobTest, ExecuteSubJob)
// {
//     MOCKER_CPP(&LivemountServiceJob::IdentifyRepos)
//         .stubs()
//         .will(returnValue(FAILED))
//         .then(returnValue(SUCCESS));
//     SetJobInfo();
//     EXPECT_EQ(m_livemountServiceJob->ExecuteSubJob(), FAILED);
//     EXPECT_EQ(m_livemountServiceJob->ExecuteSubJob(), SUCCESS);
// }

// /*
//  * 用例名称: IdentifyRepos
//  * 前置条件：
//  * check点：识别cache repo , pre data repo , cur data repo
//  */
// TEST_F(LivemountServiceJobTest, IdentifyRepos)
// {
//     EXPECT_EQ(m_livemountServiceJob->IdentifyRepos(), FAILED);
//     SetJobInfo();
//     EXPECT_EQ(m_livemountServiceJob->IdentifyRepos(), SUCCESS);
// }

// /*
//  * 用例名称: PostJob
//  * 前置条件：
//  * check点：后置处理成功
//  */
// TEST_F(LivemountServiceJobTest, PostJob)
// {
//     EXPECT_EQ(m_livemountServiceJob->PostJob(), SUCCESS);
// }